{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "b07375bb",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "import time\n",
    "import re\n",
    "from selenium import webdriver\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "7c096545",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 1.访问网址并手动模拟登录\n",
    "browser = webdriver.Chrome()\n",
    "url = 'https://runbaiyan.tmall.com/search.htm?spm=a1z10.1-b-s.w5001-21884551106.3.3f0d7f68FOYkqz&search=y&scene=taobao_shop'\n",
    "browser.get(url)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "e9f81853",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>序号</th>\n",
       "      <th>股东名称</th>\n",
       "      <th>持股比例</th>\n",
       "      <th>认缴出资额</th>\n",
       "      <th>认缴出资日期</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>华能资本服务有限公司  股权结构 &gt;  大股东</td>\n",
       "      <td>67.92149%持股详情&gt;</td>\n",
       "      <td>420,743.6万(元)</td>\n",
       "      <td>2018-12-20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2</td>\n",
       "      <td>乌江能源  贵州乌江能源投资有限公司  股权结构 &gt;  曾用名</td>\n",
       "      <td>31.47813%持股详情&gt;</td>\n",
       "      <td>194,993.1万(元)</td>\n",
       "      <td>2018-12-20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>3</td>\n",
       "      <td>人保投资控股有限公司  股权结构 &gt;</td>\n",
       "      <td>0.16304%持股详情&gt;</td>\n",
       "      <td>1,010.0万(元)</td>\n",
       "      <td>2018-12-20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4</td>\n",
       "      <td>技术改造  贵州省技术改造投资有限责任公司  股权结构 &gt;  曾用名</td>\n",
       "      <td>0.16104%持股详情&gt;</td>\n",
       "      <td>997.6万(元)</td>\n",
       "      <td>2018-12-20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>5</td>\n",
       "      <td>有色金属  中国有色金属工业贵阳有限责任公司  股权结构 &gt;  曾用名</td>\n",
       "      <td>0.09178%持股详情&gt;</td>\n",
       "      <td>568.5万(元)</td>\n",
       "      <td>2018-12-20</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>6</td>\n",
       "      <td>中国华融资产管理股份有限公司  股权结构 &gt;</td>\n",
       "      <td>0.08477%持股详情&gt;</td>\n",
       "      <td>525.1万(元)</td>\n",
       "      <td>2010-11-18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>7</td>\n",
       "      <td>首钢水城钢铁(集团)有限责任公司  股权结构 &gt;  曾用名</td>\n",
       "      <td>0.06857%持股详情&gt;</td>\n",
       "      <td>424.8万(元)</td>\n",
       "      <td>2010-11-18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>8</td>\n",
       "      <td>开磷  贵州开磷有限责任公司  股权结构 &gt;</td>\n",
       "      <td>0.03117%持股详情&gt;</td>\n",
       "      <td>193.1万(元)</td>\n",
       "      <td>2010-11-18</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   序号                                 股东名称            持股比例          认缴出资额  \\\n",
       "0   1              华能资本服务有限公司  股权结构 >  大股东  67.92149%持股详情>  420,743.6万(元)   \n",
       "1   2      乌江能源  贵州乌江能源投资有限公司  股权结构 >  曾用名  31.47813%持股详情>  194,993.1万(元)   \n",
       "2   3                   人保投资控股有限公司  股权结构 >   0.16304%持股详情>    1,010.0万(元)   \n",
       "3   4   技术改造  贵州省技术改造投资有限责任公司  股权结构 >  曾用名   0.16104%持股详情>      997.6万(元)   \n",
       "4   5  有色金属  中国有色金属工业贵阳有限责任公司  股权结构 >  曾用名   0.09178%持股详情>      568.5万(元)   \n",
       "5   6               中国华融资产管理股份有限公司  股权结构 >   0.08477%持股详情>      525.1万(元)   \n",
       "6   7        首钢水城钢铁(集团)有限责任公司  股权结构 >  曾用名   0.06857%持股详情>      424.8万(元)   \n",
       "7   8               开磷  贵州开磷有限责任公司  股权结构 >   0.03117%持股详情>      193.1万(元)   \n",
       "\n",
       "       认缴出资日期  \n",
       "0  2018-12-20  \n",
       "1  2018-12-20  \n",
       "2  2018-12-20  \n",
       "3  2018-12-20  \n",
       "4  2018-12-20  \n",
       "5  2010-11-18  \n",
       "6  2010-11-18  \n",
       "7  2010-11-18  "
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 2.获取网页源代码\n",
    "data = browser.page_source\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "245b8e1e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "华能资本服务有限公司\n"
     ]
    }
   ],
   "source": [
    "# 3.正则表达式提取相关信息\n",
    "p_sales = '<span class=\"sale-num\">(.*?)</span>'\n",
    "sales = re.findall(p_sales, data, re.S)  # 偶尔有时候会有换行，所以得加上re.S忽略换行的影响\n",
    "p_price = '<span class=\"c-price\">(.*?)</span>'\n",
    "price = re.findall(p_price, data, re.S)\n",
    "p_name = '<a class=\"item-name J_TGoldData\".*?>(.*?)</a>'  # 2021版：还是定位标题那块的网页源代码编写正则好一些\n",
    "name = re.findall(p_name, data, re.S)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f78aded1",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 4.数据清洗及打印\n",
    "for i in range(len(name)):\n",
    "    name[i] = name[i].strip()  # 清除有的标题两旁的空格\n",
    "    price[i] = price[i].strip()  # 清除价格两旁的空格\n",
    "    print('商品名称为：' + name[i])\n",
    "    print('商品价格为：' + price[i])\n",
    "    print('商品销量为：' + sales[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f843a0af",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 5.数据导出到Excel:写法2（个人更喜欢一些）:通过列表构造法\n",
    "file_name = time.strftime('%Y-%m-%d') + '润百颜销量情况_2.xlsx'\n",
    "print(file_name)  # 打印下构造的文件名\n",
    "\n",
    "df = pd.DataFrame()\n",
    "df['名称'] = name\n",
    "df['销量'] = sales\n",
    "df['价格'] = price\n",
    "\n",
    "df.to_excel(file_name, index=False)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
